package com.flannery.keyboradlibrary;

import android.content.Context;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/12/18.
 */

public class Keyboard extends LinearLayout implements View.OnClickListener {


    public interface onKeyboardListener {
        public void onZeroPressed();

        public void onOnePressed();

        public void onTwoPressed();

        public void onThreePressed();

        public void onFourPressed();

        public void onFivePressed();

        public void onSixPressed();

        public void onSevenPressed();

        public void onEightPressed();

        public void onNinePressed();

        // + - * / ok
        public void onAddPressed();

        public void onSubPressed();

        public void onMultiPressed();

        public void onDiviPressed();

        public void onClearPressed();

        public void onOkPressed();
    }

    private onKeyboardListener listener;
    private Vibrator vibrator;

    public Keyboard(Context context) {
        super(context);
        initView();
    }

    public Keyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public Keyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        // 设置布局 xml?
        View view = LayoutInflater.from(getContext()).inflate(R.layout.keyboard_layout, null);
        TextView keyboard_id_0 = (TextView) view.findViewById(R.id.keyboard_id_0);
        TextView keyboard_id_1 = (TextView) view.findViewById(R.id.keyboard_id_1);
        TextView keyboard_id_2 = (TextView) view.findViewById(R.id.keyboard_id_2);
        TextView keyboard_id_3 = (TextView) view.findViewById(R.id.keyboard_id_3);
        TextView keyboard_id_4 = (TextView) view.findViewById(R.id.keyboard_id_4);
        TextView keyboard_id_5 = (TextView) view.findViewById(R.id.keyboard_id_5);
        TextView keyboard_id_6 = (TextView) view.findViewById(R.id.keyboard_id_6);
        TextView keyboard_id_7 = (TextView) view.findViewById(R.id.keyboard_id_7);
        TextView keyboard_id_8 = (TextView) view.findViewById(R.id.keyboard_id_8);
        TextView keyboard_id_9 = (TextView) view.findViewById(R.id.keyboard_id_9);
        TextView keyboard_id_add = (TextView) view.findViewById(R.id.keyboard_id_add);
        TextView keyboard_id_sub = (TextView) view.findViewById(R.id.keyboard_id_sub);
        TextView keyboard_id_mul = (TextView) view.findViewById(R.id.keyboard_id_mul);
        TextView keyboard_id_div = (TextView) view.findViewById(R.id.keyboard_id_div);
        TextView keyboard_id_clear = (TextView) view.findViewById(R.id.keyboard_id_clear);
        TextView keyboard_id_ok = (TextView) view.findViewById(R.id.keyboard_id_ok);

        keyboard_id_0.setOnClickListener(this);
        keyboard_id_1.setOnClickListener(this);
        keyboard_id_2.setOnClickListener(this);
        keyboard_id_3.setOnClickListener(this);
        keyboard_id_4.setOnClickListener(this);
        keyboard_id_5.setOnClickListener(this);
        keyboard_id_6.setOnClickListener(this);
        keyboard_id_7.setOnClickListener(this);
        keyboard_id_8.setOnClickListener(this);
        keyboard_id_9.setOnClickListener(this);
        keyboard_id_add.setOnClickListener(this);
        keyboard_id_sub.setOnClickListener(this);
        keyboard_id_mul.setOnClickListener(this);
        keyboard_id_div.setOnClickListener(this);
        keyboard_id_clear.setOnClickListener(this);
        keyboard_id_ok.setOnClickListener(this);
        addView(view);
    }

    /**
     * need android.permission.android.permission.VIBRATE
     */
    public void setVibrator(boolean vibrator) {
        this.vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void disposeVibrator() {
        if (vibrator != null) {
            vibrator.cancel();
        }
    }

    public void setOnKeyboardListener(onKeyboardListener listener) {
        this.listener = listener;
    }

    private boolean checkoutListenerisValide() {
        if (listener != null) {
            return true;
        }
        return false;
    }

    private void pressedBtn(View view) {
//        float fromX 动画起始时 X坐标上的伸缩尺寸
//        float toX 动画结束时 X坐标上的伸缩尺寸
//        float fromY 动画起始时Y坐标上的伸缩尺寸
//        float toY 动画结束时Y坐标上的伸缩尺寸
//        int pivotXType 动画在X轴相对于物件位置类型
//        float pivotXValue 动画相对于物件的X坐标的开始位置
//        int pivotYType 动画在Y轴相对于物件位置类型
//        float pivotYValue 动画相对于物件的Y坐标的开始位置
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.9f, 1f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);
        view.startAnimation(scaleAnimation);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        pressedBtn(view);
        if (this.vibrator != null) {
            this.vibrator.vibrate(200);
            vibrator.vibrate(new long[]{100}, -1);
        }
        if (id == R.id.keyboard_id_0) {
            if (checkoutListenerisValide()) listener.onZeroPressed();
        } else if (id == R.id.keyboard_id_1) {
            if (checkoutListenerisValide()) listener.onOnePressed();
        } else if (id == R.id.keyboard_id_2) {
            if (checkoutListenerisValide()) listener.onTwoPressed();
        } else if (id == R.id.keyboard_id_3) {
            if (checkoutListenerisValide()) listener.onThreePressed();
        } else if (id == R.id.keyboard_id_4) {
            if (checkoutListenerisValide()) listener.onFourPressed();
        } else if (id == R.id.keyboard_id_5) {
            if (checkoutListenerisValide()) listener.onFivePressed();
        } else if (id == R.id.keyboard_id_6) {
            if (checkoutListenerisValide()) listener.onSixPressed();
        } else if (id == R.id.keyboard_id_7) {
            if (checkoutListenerisValide()) listener.onSevenPressed();
        } else if (id == R.id.keyboard_id_8) {
            if (checkoutListenerisValide()) listener.onEightPressed();
        } else if (id == R.id.keyboard_id_9) {
            if (checkoutListenerisValide()) listener.onNinePressed();
        } else if (id == R.id.keyboard_id_add) {
            if (checkoutListenerisValide()) listener.onAddPressed();
        } else if (id == R.id.keyboard_id_sub) {
            if (checkoutListenerisValide()) listener.onSubPressed();
        } else if (id == R.id.keyboard_id_mul) {
            if (checkoutListenerisValide()) listener.onMultiPressed();
        } else if (id == R.id.keyboard_id_div) {
            if (checkoutListenerisValide()) listener.onDiviPressed();
        } else if (id == R.id.keyboard_id_clear) {
            if (checkoutListenerisValide()) listener.onClearPressed();
        } else if (id == R.id.keyboard_id_ok) {
            if (checkoutListenerisValide()) listener.onOkPressed();
        }
    }
}
